package org.manager.hsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.hsqldb.Server;

public class HsqlListener implements ServletContextListener {

	private int port = -1;
	private String dbName;
	   
/*	public void contextInitialized(ServletContextEvent sce) {
		
		dbName = sce.getServletContext().getInitParameter("hsql.dbName");    
		String path = sce.getServletContext().getInitParameter("hsql.dbPath");
		String sport = sce.getServletContext().getInitParameter("hsql.port");
		
		if(dbName == null || dbName.equals("")){
			dbName = "myDB";
		}
		
		//如果没有指定数据库路径，就是用应用根目录下的数据文件.
		if(path == null || path.equals("")){
			path = sce.getServletContext().getRealPath("/");
		}
		
		if(sport == null || sport.equals("")){
			sport = "9001";
		}
		
	   
		try {
			port = Integer.parseInt(sport);    
		}catch(Exception e){
			port = 9001;
		}

		if (dbName == null || dbName.equals("")){ 
			System.out.println("Cant' get hsqldb.dbName from web.xml Context Param");    
			return;
		}

		File dbDir = new File(path);    

		if (!dbDir.exists()) {//判断目录是否存在    
			if (!dbDir.mkdirs()) {//如果不存在创建，如果创建失败直接返回    
				System.out.println("Can not create DB Dir for Hsql:" + dbDir);    
					return;
			}
		}
		
		if (!path.endsWith("/")){
			path = path + "/"; 
		}

		File scriptFile = new File(path + dbName + ".script");    
		File propertiesFile = new File(path + dbName + ".properties");    

		if (scriptFile.exists() && propertiesFile.exists()){//判断数据文件是否存在
			this.startServer(path, dbName, port);
		}else{
			System.out.println("数据库启动失败，数据文件不存在!");
		}
	}
	*/
	
	
	public void contextInitialized(ServletContextEvent sce){
		
	}
	   
	/**   
	  　* 启动Hsqldb服务的方法。   
	  　* @param dbPath　数据库路径   
	  　* @param dbName　数据库名称   
	  　* @param port　所使用的端口号   
	  　*/   
	@SuppressWarnings("unused")
	private void startServer(String dbPath, String dbName, int port) {  
	   
		Server server = new Server();//它可是hsqldb.jar里面的类啊。    
		server.setDatabaseName(0, dbName);    
		server.setDatabasePath(0, dbPath + dbName);    
	   
		if (port != -1){    
			server.setPort(port);    
		}
	   
		server.setSilent(true);
		server.start();    
		System.out.println("hsqldb started...");    
		//等待Server启动    
		try {
			Thread.sleep(800);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	   
	/**   
	 　　* Listener销毁方法，在Web应用终止的时候执行"shutdown"命令关闭数据库.   
	 　　*/   
	public void contextDestroyed(ServletContextEvent arg0){
		Connection conn = null;    
		try {
			Class.forName("org.hsqldb.jdbcDriver");
			conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:"+port+"/"+dbName, "sa", "");    
			Statement stmt = conn.createStatement();    
			stmt.executeUpdate("SHUTDOWN;");
		} catch (Exception e) {
			e.printStackTrace();
		}    
		  
	}


}
